import {equals, pipe, prop, reject} from 'ramda'

export const getCurrentUser = () => {
    let users = window.localStorage.getItem('users');
    if (users) {
        users = JSON.parse(users) || [];
        return users[users.length - 1];
    } else {
        return null
    }
}

export const setCurrentUser = (currentUser) => {
    let users = window.localStorage.getItem('users');
    users = users ? JSON.parse(users) : [];
    const index = users.findIndex(x => (x.companyId == currentUser.companyId || x.candidateId == currentUser.candidateId));
    if (index != -1) {
        users = users.filter(x => x.companyId != currentUser.companyId && x.candidateId != currentUser.candidateId)
    }
    users.push(currentUser)
    window.localStorage.setItem('users', JSON.stringify(users))
}

export const isUserExist = (currentUser) => {
    let users = window.localStorage.getItem('users');
    if (users) {
        users = JSON.parse(users) || [];
        const index = users.findIndex(x => (x.companyId == currentUser.companyId || x.candidateId == currentUser.candidateId));
        return index > 0 ? true : false
    }
    return false
}

export const getToken = () => window.localStorage.getItem('token');
export const setToken = (token) => window.localStorage.setItem('token', token);

